package pattern.strategy;

/**
 * 冒泡排序执行类
 *
 * @author sun.ye
 * @since 2020-07-15 18:14
 */
public class BubbleSorter<T> {

    private int operations;
    private int length;
    private SortHandle<T> handle;

    public BubbleSorter(SortHandle<T> handle) {
        this.handle = handle;
    }


    public void sort(T[] array){
        handle.setArray(array);
        length = handle.length();
        for (int nextToLast = length - 2; nextToLast >= 0 ; nextToLast--) {
            for (int i = 0; i <= nextToLast ; i++) {
                if(handle.outOfOrder(i)){
                    handle.swap(i);
                }
            }
        }
    }
}